import time
import uuid

from fastapi import FastAPI

app = FastAPI()
from pydantic import BaseModel
from datetime import datetime
import uvicorn

from concurrent.futures import ThreadPoolExecutor

executor = ThreadPoolExecutor(max_workers=100)


# 定义一个任务函数
def task(name):
    print(f"Task {name} started")
    time.sleep(5)
    id=uuid.uuid4()
    return f"{id}"


class MyClassModel(BaseModel):
    question: str


@app.post("/test")
def create_myclass(myclass: MyClassModel):
    t1 = datetime.now()

    question = myclass.question
    print(question)

    future = executor.submit(task, question)
    # 获取任务执行结果
    print(future.result())

    result = future.result()

    t2 = datetime.now()
    second = (t1 - t2).total_seconds()

    print(f"耗时{second}")
    return {"answer": result}


if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8093)
